package com.hu.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hu.common.ErrorCode;
import com.hu.dao.QuestionBankDao;
import com.hu.dao.QuestionBankQuestionDao;
import com.hu.dao.QuestionDao;
import com.hu.exception.ThrowUtils;
import com.hu.pojo.dto.questionbank.QuestionBankQueryRequest;
import com.hu.pojo.entity.QuestionBank;
import com.hu.pojo.entity.QuestionBankQuestion;
import com.hu.service.QuestionBankService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author 20272
* @description 针对表【question_bank(题库)】的数据库操作Service实现
* @createDate 2024-09-24 22:41:22
*/
@Service
@Slf4j
@RequiredArgsConstructor
public class QuestionBankServiceImpl implements QuestionBankService {


    private final QuestionBankDao questionBankDao;

    private final QuestionBankQuestionDao questionBankQuestionDao;

    private final QuestionDao questionDao;
    /**
     * 校验数据
     *
     * @param questionBank
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validQuestionBank(QuestionBank questionBank, boolean add) {
        ThrowUtils.throwIf(questionBank == null, ErrorCode.PARAMS_ERROR);
        // todo 从对象中取值
        String title = questionBank.getTitle();
        // 创建数据时，参数不能为空
        if (add) {
            // todo 补充校验规则
            ThrowUtils.throwIf(StringUtils.isBlank(title), ErrorCode.PARAMS_ERROR);
        }
        // 修改数据时，有参数则校验
        // todo 补充校验规则
        if (StringUtils.isNotBlank(title)) {
            ThrowUtils.throwIf(title.length() > 80, ErrorCode.PARAMS_ERROR, "标题过长");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(QuestionBank questionBank) {
        boolean save = questionBankDao.save(questionBank);
        ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
    }

    @Override
    public QuestionBank getQuestionBankById(long id) {
        return questionBankDao.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeQuestionBankById(long id) {
        questionBankQuestionDao.removeQuestionBankQuestionByBankId(id);
        questionBankDao.removeById(id);
    }

    @Override
    public boolean updateById(QuestionBank questionBank) {
        return questionBankDao.updateById(questionBank);
    }

    @Override
    public Page<QuestionBank> pages(QuestionBankQueryRequest questionBankQueryRequest) {
        long current = questionBankQueryRequest.getCurrent();
        long size = questionBankQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 200, ErrorCode.PARAMS_ERROR);
        // 根据tag来筛选出题目
        List<Long> questionIds = questionDao.findQuestionIdsByTag(questionBankQueryRequest.getTag());
        // 根据题目的id来筛选出题库
        Page<QuestionBankQuestion> page = new Page<>(current, size);
        page = questionBankQuestionDao.findQuestionBankIdsByQuestionIds(page,questionIds);
        List<QuestionBankQuestion> records = page.getRecords();
        Set<Long> questionBankIds = new HashSet<>();
        if (CollUtil.isNotEmpty(records)){
            questionBankIds = records.stream().map(QuestionBankQuestion::getQuestionBankId).collect(Collectors.toSet());
        }
        Page<QuestionBank> bankPage = new Page<>(current, size);
        return questionBankDao.findQuestionBankPageByIds(bankPage,questionBankIds);
    }

    @Override
    @Transactional
    public Boolean checkProcessAndUpdate(QuestionBank questionBank) {
        return questionBankDao.updateById(questionBank);
    }
}




